package com.pospi.util;

import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.lany.sp.SPHelper;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dto.LoginReturnDto;
import com.pospi.dto.VipBeen;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.login.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by acer on 2018/11/13.
 */

public class NetUtil {
    public static void searchVip(String num , final Handler handler) {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getvipinfo");

        JSONObject object = new JSONObject();
        try {
            object.put("num", num);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                LoginReturnDto dto = new HttpConnection().parseJson(Response);
                if (dto == null) {
                    Message message = Message.obtain();
                    message.what = 3;
                    handler.sendMessage(message);
                } else {
                    if (dto.getErrCode().equals("100")) {
                        VipBeen lsVipBeen = new Gson().fromJson(dto.getResult(), VipBeen.class);
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = lsVipBeen;
                        handler.sendMessage(message);
                    } else {
                        Message message = Message.obtain();
                        message.what = 3;
                        message.obj = dto.getErrMsg();
                        handler.sendMessage(message);
                    }
                }

            }
        });
    }
    public static void hyCz(String num , final Handler handler , VipBeen lsVipBeen) {
        double je = DoubleSave.doubleSaveTwo(Double.parseDouble(lsVipBeen.getMzye()) + Double.parseDouble(num));
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "vip.Vvip");
        params.put("fun", "savecardcz");
        params.put("table", "vip_info");

        JSONObject object = new JSONObject();
        try {
            object.put("mzye", je);
            object.put("id", lsVipBeen.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                LoginReturnDto dto = new HttpConnection().parseJson(Response);
                if (dto.getErrCode().equals("100")) {
                    Message message = Message.obtain();
                    message.what = 4;
                    handler.sendMessage(message);
                } else {
                    Message message = Message.obtain();
                    message.what = 5;
                    handler.sendMessage(message);
                }
            }
        });
    }


}
